# -*- coding: utf-8 -*-
# @Time        :2024/11/5 上午10:53
# @Author      :文刀水寿
# @File        : 02_装饰器.py
"""
 @Description :装饰器也是一种闭包，其功能在不破坏目标函数原有的代码和功能的前提下，为目标函数增加新功能
"""


def sleep():
    import random
    import time
    print("睡眠中......")
    time.sleep(random.randint(1, 5))


def outer(func):
    def inner():
        print("我要睡觉了")
        func()
        print("我要起床了")

    return inner


fn = outer(sleep)
fn()


# 装饰器的快捷写法
def outer(func):
    def inner():
        print("我要睡觉了")
        func()
        print("我要起床了")

    return inner


@outer
def sleep():
    import random
    import time
    print("睡眠中......")
    time.sleep(random.randint(1, 5))


sleep()
